/**
 * 1.swig编译html模板
 * 2.useref合并html引用的资源文件成一个新文件，html模板需要寻找相应的注释
  <!-- build:<type> <path>  -->
  ... HTML Markup, list of script / link tags.
  <!-- endbuild -->
  例如：
  <!-- build:js assets/scripts/vendor.js -->
  <script src="/node_modules/jquery/dist/jquery.js"></script>
  <script src="/node_modules/popper.js/dist/umd/popper.js"></script>
  <script src="/node_modules/bootstrap/dist/js/bootstrap.js"></script>
  <!-- endbuild -->

 */

const { src, dest, watch, series, parallel } = require('gulp')

const loadPlugins = require('gulp-load-plugins')
const plugins = loadPlugins()

const browserSync = require('browser-sync')
const bs = browserSync.create()

const del = require('del')

// const sass = require('gulp-sass')
// const babel = require('gulp-babel')
// const swig = require('gulp-swig')
// const imagemin = require('gulp-imagemin')
// const useref = require('gulp-useref')
// const gulpIf = require('gulp-if')

const data = {
  menus: [
    {
      name: 'Home',
      icon: 'aperture',
      link: 'index.html'
    },
    {
      name: 'Features',
      link: 'features.html'
    },
    {
      name: 'About',
      link: 'about.html'
    },
    {
      name: 'Contact',
      link: '#',
      children: [
        {
          name: 'Twitter',
          link: 'https://twitter.com/w_zce'
        },
        {
          name: 'About',
          link: 'https://weibo.com/zceme'
        },
        {
          name: 'divider'
        },
        {
          name: 'About',
          link: 'https://github.com/zce'
        }
      ]
    }
  ],
  pkg: require('./package.json'),
  date: new Date()
}


// 清除文件
const clean = () => {
  return del(['dist', 'temp'])
}

// 样式
const style = () => {
  return src('src/assets/styles/*.scss', { base: 'src' })
  .pipe(plugins.sass({ outputStyle: 'expanded' }))
  .pipe(dest('temp'))
  .pipe(bs.reload({ stream: true }))
}

// js
const script = () => {
  return src('src/assets/scripts/*.js', { base: 'src' })
  .pipe(plugins.babel({ presets: ['@babel/preset-env']}))
  .pipe(dest('temp'))
  .pipe(bs.reload({ stream: true }))
}

// page
const page = () => {
  return src('src/*.html', { base: 'src' })
  .pipe(plugins.swig({ data }))
  .pipe(dest('temp'))
  .pipe(bs.reload({ stream: true }))
}

// image
const image = () => {
  return src('src/assets/images/**', { base: 'src' })
  .pipe(plugins.imagemin())
  .pipe(dest('dist'))
}

// font
const font = () => {
  return src('src/assets/fonts/**', { base: 'src' })
  .pipe(plugins.imagemin())
  .pipe(dest('dist'))
}

// extra
const extra = () => {
  return src('public/**', { base: 'public' })
  .pipe(dest('dist'))
}

// serve
const serve = () => {
  watch('src/assets/scripts/*.js', script)
  watch('src/assets/styles/*.scss', style)
  watch('src/**/*.html', page)
  watch([
    'src/assets/images/**',
    'src/assets/fonts/**',
    'public/**'
  ], bs.reload)

  bs.init({
    notify: false,
    // open: false,
    // files: 'temp/**', // 监控文件热更新用bs.reload()代替
    server: {
      baseDir: ['temp', 'src', 'public'],
      routes: {
        '/node_modules': 'node_modules'
      }
    }
  })
}

// 合并资源文件
const useref = () => {
  return src('temp/*.html', { base: 'temp' })
  // searchPath合并哪个目录下的资源文件
  .pipe(plugins.useref({ searchPath: ['temp', '.'] }))
  // html js css压缩
  .pipe(plugins.if(/\.js$/, plugins.uglify()))
  .pipe(plugins.if(/\.css$/, plugins.cleanCss()))
  // htmlmin 默认只针对一些属性的空格，换行并不压缩
  .pipe(plugins.if(/\.html$/, plugins.htmlmin({
    // 压缩换行
    collapseWhitespace: true, 
    // 压缩行间js
    minifyCSS: true,
    // 压缩行间css
    minifyJS: true
  })))
  .pipe(dest('dist'))
}

const compile = parallel(style, script, page)

const develop = series(compile, serve)

const build = series(clean, parallel(series(compile, useref), image, font, extra))



module.exports = {
  develop,
  clean,
  build,
}